package main;

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

/**
 * 
 * @author Peng
 *
 */

public class Border extends JFrame{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public int ROWS;
	public int COLS;
	public int GAP=3;
	public JPanel mainPanel;
	public JPanel[][] smallPanelContainer; 	//Total 9 of them, each contains 9 small panels.
	public JPanel[][] smallPanels;			//Total 81 of them, each panel represents a grid. 
	public JLabel[][] panelLabel;			//Total 81 of them, each label used for digit display.
	
	public GridLayout gridlayout;
	
	// Creates a menubar for a JFrame	      
	public JMenuBar menuBar = new JMenuBar();
	   
	// Define and add two drop down menu to the menubar	   
	public JMenu mainMenu = new JMenu("Menu");
	public JMenu levelMenu = new JMenu("Level");
	   
	   
	// Create and add simple menu item to one of the drop down menu	   
	public JMenuItem newAction = new JMenuItem("New Game");
	public JMenuItem saveAction = new JMenuItem("Save");
	public JMenuItem exitAction = new JMenuItem("Exit");
	   
	   	   
	   
	// Create and add CheckButton as a menu item to one of the drop down	      
	// menu	   
	public JCheckBoxMenuItem esayLevel = new JCheckBoxMenuItem("esay");	   
	public JCheckBoxMenuItem medianLevel = new JCheckBoxMenuItem("median");	   
	public JCheckBoxMenuItem hardLevel = new JCheckBoxMenuItem("hard");
	
	public Color color1 = Color.WHITE;
	public Color color2 = Color.BLACK;
	public Color color3 = Color.BLUE;
	public Color color4 = new Color(211,211,211);
	public Color color5 = Color.RED;
	

	public Border(String name)
	{
		super(name);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(400,400);
	}
	
	public void setBorderSize(int row, int col)
	{
		ROWS=row;
		COLS=col;
	}

	
	/**
	 * Creates the Border, 81 grids which composed of 9 big grids each contains another 9 small grids.
	 * @param rows, the number of rows.
	 * @param cols, the number of columns.
	 * @param gap, the gap between rows and columns.
	 */
	public void createBorder()
	{
		int smallRows=ROWS/3;
		int smallCols=COLS/3;
		int size = smallRows;
		gridlayout=new GridLayout(smallRows,smallCols, GAP, GAP);
		panelLabel=new JLabel[ROWS][COLS];
		mainPanel = new JPanel(gridlayout);
		smallPanelContainer = new JPanel[smallRows][smallCols];
		smallPanels = new JPanel[ROWS][COLS];
		
		//Creating 81 small panels each represents a grid
		for(int r=0; r<ROWS; r++)
		{
			for(int c=0; c<COLS; c++)
			{
				smallPanels[r][c] = new JPanel();
				panelLabel[r][c] = new JLabel();
				smallPanels[r][c].add(panelLabel[r][c]);
				//mainPanel.add(panel[r][c]);
			}
		}
		
		//grouping 9 small grids as one big grid
		for(int r=0; r<smallRows; r++)
		{
			for(int c=0; c<smallCols; c++)
			{
				createInternalBorder(size, r, c);
			}
		}
		add(mainPanel);
	}
	
	/**
	 * Create the internal border, one of the 9 big grids in the sudoku border.
	 * @param size, 3x3
	 * @param row, the row number
	 * @param col, the column number
	 */
	public void createInternalBorder(int size, int row, int col)
	{
		//int index=0;
		smallPanelContainer[row][col] = new JPanel(new GridLayout(size,size,1,1));
		smallPanelContainer[row][col].setBackground(color2);
		for(int r=0; r<size; r++)
		{
			for(int c=0; c<size; c++)
			{
				smallPanelContainer[row][col].add(smallPanels[r+size*row][c+size*col]);
			}
		}
		mainPanel.add(smallPanelContainer[row][col]);
	}
	
	/**
	 * @param r the row index.
	 * @param c the column index.
	 * @return true if the indexes of row and column are within the Border range.
	 */
	public boolean isInRange(int r, int c)
	{
		if(r>=0 && r<=ROWS-1 && c>=0 && c<=COLS-1)
			return true;
		else
			return false;	
	}

}
